/*
 * Main.c
 *
 *  Created on: 16 juli 2021
 *      Author: Daniel Mårtensson
 */

#include <stdio.h>

/* Include Open SAE J1939 */
#include "Open_SAE_J1939/Open_SAE_J1939.h"

int main() {

	/* Create our J1939 structure with two ECU */
	J1939 j1939_1 = {0};
	J1939 j1939_2 = {0};

	/* Important to sent all non-address to 0xFF - Else we cannot use ECU address 0x0 */
	uint8_t i;
	for(i = 0; i < 255; i++){
		j1939_1.other_ECU_address[i] = 0xFF;
		j1939_2.other_ECU_address[i] = 0xFF;
	}

	/* Set the ECU address */
	j1939_1.information_this_ECU.this_ECU_address = 0xA;												/* From 0 to 253 because 254 = error address and 255 = broadcast address */
	j1939_2.information_this_ECU.this_ECU_address = 0x90;

	/* Set NAME for ECU 1 */
	j1939_1.information_this_ECU.this_name.identity_number = 100; 										/* From 0 to 2097151 */
	j1939_1.information_this_ECU.this_name.manufacturer_code = 300; 									/* From 0 to 2047 */
	j1939_1.information_this_ECU.this_name.function_instance = 10; 										/* From 0 to 31 */
	j1939_1.information_this_ECU.this_name.ECU_instance = 2; 											/* From 0 to 7 */
	j1939_1.information_this_ECU.this_name.function = FUNCTION_VDC_MODULE;								/* From 0 to 255 */
	j1939_1.information_this_ECU.this_name.vehicle_system = 100;										/* From 0 to 127 */
	j1939_1.information_this_ECU.this_name.arbitrary_address_capable = 0;								/* From 0 to 1 */
	j1939_1.information_this_ECU.this_name.industry_group = INDUSTRY_GROUP_CONSTRUCTION;				/* From 0 to 7 */
	j1939_1.information_this_ECU.this_name.vehicle_system_instance = 10;								/* From 0 to 15 */

	/* Set NAME for ECU 2 */
	j1939_2.information_this_ECU.this_name.identity_number = 1000; 										/* From 0 to 2097151 */
	j1939_2.information_this_ECU.this_name.manufacturer_code = 400; 									/* From 0 to 2047 */
	j1939_2.information_this_ECU.this_name.function_instance = 20; 										/* From 0 to 31 */
	j1939_2.information_this_ECU.this_name.ECU_instance = 1; 											/* From 0 to 7 */
	j1939_2.information_this_ECU.this_name.function = FUNCTION_AUXILIARY_VALVES_CONTROL;				/* From 0 to 255 */
	j1939_2.information_this_ECU.this_name.vehicle_system = 50;											/* From 0 to 127 */
	j1939_2.information_this_ECU.this_name.arbitrary_address_capable = 0;								/* From 0 to 1 */
	j1939_2.information_this_ECU.this_name.industry_group = INDUSTRY_GROUP_AGRICULTURAL_AND_FORESTRY;	/* From 0 to 7 */
	j1939_2.information_this_ECU.this_name.vehicle_system_instance = 15;								/* From 0 to 15 */

	/* Broadcast request NAME from ECU 1 to all ECU */
	SAE_J1939_Send_Request_Address_Claimed(&j1939_1, 0xFF);							/* 0xFF means broadcast to all ECU */

	/* Listen for NAME request for ECU 2 from ECU 1 */
	Open_SAE_J1939_Listen_For_Messages(&j1939_2);

	/* Listen for NAME response request from ECU 2 to ECU 1 */
	Open_SAE_J1939_Listen_For_Messages(&j1939_1);

	/* Send request NAME from ECU 2 to ECU 1 */
	SAE_J1939_Send_Request_Address_Claimed(&j1939_2, 0xA);

	/* Listen for NAME request for ECU 1 from ECU 2 */
	Open_SAE_J1939_Listen_For_Messages(&j1939_1);

	/* Listen for NAME response request from ECU 1 to ECU 2 */
	Open_SAE_J1939_Listen_For_Messages(&j1939_2);

	/* Print information */
	printf("How many external ECU are connected according to ECU 1: %i and the NAME came from address = 0x%X\n", j1939_1.number_of_other_ECU, j1939_1.from_other_ecu_name.from_ecu_address);
	printf("How many external ECU are connected according to ECU 2: %i and the NAME came from address = 0x%X\n", j1939_2.number_of_other_ECU, j1939_2.from_other_ecu_name.from_ecu_address);

	return 0;
}
